using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using ThiefRun.Util.Keyboards;
using System.Diagnostics;
using ThiefRun.States;

namespace ThiefRun
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class ThiefRunGame : Microsoft.Xna.Framework.Game
    {
        #region Constants
        // Constants
        const string LEVEL_DIR = "Levels";
        #endregion Constants

        #region Properties
        // Properties
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        KeyboardManager keyboardManager;
        StateMachine stageMachine;
        #endregion Properties

        #region Computed Properties
        // Computed Properties
        public GraphicsDeviceManager Graphics { get { return graphics; } }
        #endregion Computed Properties

        #region Methods
        // Methods
        public ThiefRunGame()
        {
            graphics = new GraphicsDeviceManager(this);

            graphics.IsFullScreen = true;
            Content.RootDirectory = "Content";

            keyboardManager = new KeyboardManager();

            List<Keys> keys = new List<Keys>();
            keys.Add(Keys.Escape);

            keyboardManager.Add(keys, ExitAction);

            stageMachine = new StateMachine();
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {   
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            // TODO: use this.Content to load your game content here
            Transition.Conditions titleCondition = TitleCondition;
            Transition.Conditions gameOverCondition = GameOverCondition;
            Transition.Conditions winCondition = WinCondition;
            TitleState titleState = new TitleState(this);
            LevelState levelState = new LevelState(this);
            GameOverState gameOverState = new GameOverState(this);
            WinState winState = new WinState(this);
       
            stageMachine.AddNode(titleState);
            stageMachine.AddNode(levelState);
            stageMachine.AddNode(gameOverState);
            stageMachine.AddNode(winState);
            stageMachine.AddTransition(titleState, levelState, titleCondition);
            stageMachine.AddTransition(levelState, gameOverState, gameOverCondition);
            stageMachine.AddTransition(levelState, winState, winCondition);

            stageMachine.SetCurrent = titleState;
        }
        
        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // TODO: Add your update logic here
            keyboardManager.Update(gameTime);
            
            stageMachine.Update(gameTime);
            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            // TODO: Add your drawing code here
            
            stageMachine.Draw(gameTime, spriteBatch);
            base.Draw(gameTime);
        }

        #region Keyboard Actions
        /// <summary>
        /// The action to execute when the escape is pressed.
        /// </summary>
        public void ExitAction() { this.Exit(); }
        #endregion Keyboard Actions

        #region State Conditions
        // Actions
        protected bool TitleCondition(State caller)
        {
            TitleState stage = (TitleState)caller;
            return (stage.MenuTime <= 0);
        }
        
        protected bool GameOverCondition(State caller)
        {
            LevelState stage = (LevelState)caller;
            return (stage.TimeLeft <= 0);
        }

        protected bool WinCondition(State caller)
        {
            LevelState stage = (LevelState)caller;
            return (stage.Player.Collides(stage.TargetTrain));
        }
        #endregion State Conditions
        #endregion Methods
    }
}
